package com.hustzp.com.xichuangzhu.poetry.dao;

import android.content.Context;

import com.hustzp.com.xichuangzhu.model.Collections;
import com.hustzp.com.xichuangzhu.model.Review;
import com.hustzp.com.xichuangzhu.poetry.model.CollectionWorks;
import com.hustzp.com.xichuangzhu.poetry.model.LikePost;
import com.hustzp.com.xichuangzhu.poetry.model.Works;
import com.hustzp.com.xichuangzhu.utils.DBHelper;
import com.j256.ormlite.stmt.QueryBuilder;

import java.util.List;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/27 21:47
 * @Description:分类列表 数据层
 */
public class CollectionKindListDao {
    private Context context;
    private DBHelper helper;

    public CollectionKindListDao(Context context) {
        this.context = context;
        try {
            helper = DBHelper.getInstance(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<CollectionWorks> getCollectioinWorks(Collections ccollections) {
        try {
            QueryBuilder<CollectionWorks, Integer> builder = (QueryBuilder<CollectionWorks, Integer>) helper.getDao(CollectionWorks.class).queryBuilder();
            builder.where().eq("collection_id", ccollections.getId());
            builder.orderBy("show_order", true);
            List<CollectionWorks> users = helper.getDao(CollectionWorks.class).query(builder.prepare());
            return users;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<CollectionWorks> getCollectionWorksCatagory(Works works) {
        try {
            QueryBuilder<CollectionWorks, Integer> builder = (QueryBuilder<CollectionWorks, Integer>) helper.getDao(CollectionWorks.class).queryBuilder();
            builder.where().eq("work_id", works.getId());
//            builder.orderBy("show_order", true);
            List<CollectionWorks> users = helper.getDao(CollectionWorks.class).query(builder.prepare());
            return users;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Review> getCollectionWorksQuotes(Works works) {
        try {
            QueryBuilder<Review, Integer> builder = (QueryBuilder<Review, Integer>) helper.getDao(Review.class).queryBuilder();
            builder.where().eq("work_id", works.getId());
            List<Review> users = helper.getDao(Review.class).query(builder.prepare());
            return users;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Collections getCollections(CollectionWorks works) {
        Collections collections = null;
        try {
            QueryBuilder<Collections, Integer> builder = (QueryBuilder<Collections, Integer>) helper.getDao(Collections.class).queryBuilder();
            builder.where().eq("id", works.getCollection_id());
            List<Collections> users = helper.getDao(Collections.class).query(builder.prepare());
            if (users != null && users.size() > 0) {
                collections = users.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return collections;
    }

    public Works getWorks(Works works) {
        Works collections = null;
        try {
            QueryBuilder<Works, Integer> builder = (QueryBuilder<Works, Integer>) helper.getDao(Works.class).queryBuilder();
            builder.where().eq("id", works.getId());
            List<Works> users = helper.getDao(Works.class).query(builder.prepare());
            if (users != null && users.size() > 0) {
                collections = users.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return collections;
    }

    public Works getWorks(LikePost post) {
        Works collections = null;
        try {
            QueryBuilder<Works, Integer> builder = (QueryBuilder<Works, Integer>) helper.getDao(Works.class).queryBuilder();
            builder.where().eq("id", post.getWORKID());
            List<Works> users = helper.getDao(Works.class).query(builder.prepare());
            if (users != null && users.size() > 0) {
                collections = users.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return collections;
    }

}
